package com.quyunshuo.module.home

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.widget.RemoteViews
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.quyunshuo.module.home.activity.main.MainActivity
import com.quyunshuo.module.home.activity.main.MainActivity.Companion

object NotificationUtils {

    // 唯一通知渠道ID（按功能分类）
    private const val CHANNEL_ID = "default_channel"
    private const val CHANNEL_NAME = "默认通知"
    private const val NOTIFICATION_ID = 1 // 唯一通知ID

    // 初始化通知渠道
    fun createNotificationChannel(context: Context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(
                CHANNEL_ID,
                CHANNEL_NAME,
                NotificationManager.IMPORTANCE_DEFAULT // 通知重要性级别
            ).apply {
                description = "重要通知通道"
            }

            val manager = context.getSystemService(NotificationManager::class.java)
            manager.createNotificationChannel(channel)
        }
    }

    // 发送通知
    @SuppressLint("RemoteViewLayout")
    fun showNotification(
        context: Context,
        title: String,
        content: String,
        smallIcon: Int = android.R.drawable.ic_dialog_info
    ) {
        // 确保渠道已创建
        createNotificationChannel(context)
        val intent = Intent(context, MainActivity::class.java).apply {
            putExtra("is_activated", false)
            putExtra("entranceKey", "EntranceNotification")
            // 3. 添加任务标志
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val flags = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
            else -> PendingIntent.FLAG_UPDATE_CURRENT
        }

        val pendingIntent = PendingIntent.getActivity(
            context,
            NOTIFICATION_ID,
            intent,
            flags
        )

        val collapsedView = RemoteViews(context.packageName, R.layout.home_layout_notification_collapsed)
        // 4. 使用不同的展开布局(如果可用)
        val expandedView = try {
            RemoteViews(context.packageName, R.layout.home_layout_notification_collapsed)
        } catch (e: Exception) {
            // 5. 如果展开布局不可用，仍使用折叠布局
            RemoteViews(context.packageName, R.layout.home_layout_notification_collapsed)
        }
        // 6. 设置点击事件 - 使用通知ID
        collapsedView.setOnClickPendingIntent(R.id.deactivate, pendingIntent)
        expandedView.setOnClickPendingIntent(R.id.deactivate, pendingIntent)
        // 构建通知
        val notification: Notification = NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(smallIcon) // 必须设置的小图标
            .setContentTitle(title)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(collapsedView)
            .setCustomBigContentView(expandedView)
            .setContentText(content)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true) // 点击后自动消失
            .build()

        // 发送通知
        with(NotificationManagerCompat.from(context)) {
            notify(NOTIFICATION_ID, notification) // 使用相同ID更新通知
        }
    }
}